<?php
/*数据库类：静态方法*/
class Db {
    private static $dbhost='';
    private static $dbport;
    private static $dbuser;
    private static $dbpwd;
    private static $dbname;
    private static $dbcharset;
    private static $dblink;
    private static $tablepre;

    public static function dbInstall(){
        self::$dbhost= $GLOBALS['config']['Db']['dbhost'];
        self::$dbport= $GLOBALS['config']['Db']['dbport'];
        self::$dbuser= $GLOBALS['config']['Db']['dbuser'];
        self::$dbpwd= $GLOBALS['config']['Db']['dbpwd'];
        self::$dbname= $GLOBALS['config']['Db']['dbname'];
        self::$dbcharset= $GLOBALS['config']['Db']['dbcharset'];
        self::$tablepre= $GLOBALS['config']['Db']['tablepre'];

        self::initial();
        return self::$dblink;
    }
    //数据库其它初始化方法
    private static function initial(){
        self::dbLink();
        self::dbCharset();
        self::dbUse();
    }
    //连接数据库方法
    private static function dbLink(){
        @self::$dblink = mysql_connect(self::$dbhost.":".self::$dbport,self::$dbuser,self::$dbpwd);
        if(!self::$dblink ){
            echo "数据库连接失败：<br />";
            echo "错误编号：".mysql_errno()."<br />";
            echo "错误原因：".mysql_error()."<br />";
            exit;
        }
    }
    //提交sql语句方法
    private static function  dbQuery($sql){
        if(strpos($sql,'#__')!==false){
            $sql = str_replace('#__',self::$tablepre,$sql);
        }

        $res = mysql_query($sql);
        if(!$res){
            echo "{$sql}语句有误：<br />";
            echo "错误编号：".mysql_errno()."<br />";
            echo "错误原因：".mysql_error()."<br />";
            exit;
        }
        return $res;
    }
    //设置字符集方法
    /**
     *
     */
    private static function dbCharset(){
        $sql = "set names ".self::$dbcharset;
        self::dbQuery($sql);
    }

    //选择数据库方法
    private static function dbUse(){
        $sql="use ".self::$dbname;
        self::dbQuery($sql);
    }
    //添加数据库方法
    /**
     * @param $sql
     * @return int
     */
    public static function Insert($table,$insert_array){
        $fields = $values = '';
        foreach($insert_array as $k=>$v){
            $fields.= "$k,";
            $values.= "'$v',";
        }
        $fields = rtrim($fields,',');
        $values = trim($values,',');
        $sql = "insert into #__$table ($fields) values ($values)";

        self::dbQuery($sql);
        return mysql_insert_id();
    }

    //更新数据库方法,返回影响行数
    public static function Update($table,$update_array,$where){
        $update_str = '';
        foreach($update_array as $k=>$v){
            $update_str.="$k='$v',";
        }
        $update_str = rtrim($update_str,',');
        $sql = "update #__$table set $update_str where $where";
        self::dbQuery($sql);
        return mysql_affected_rows();
    }
    /**
     *删除一条或多条数据
     * @param $sql
     * @return int:受影响行数
     */
    public static function Delete($table,$where){
        $sql = "delete from #__$table where $where";
        self::dbQuery($sql);
        return mysql_affected_rows();
    }

    /**
     * @param $sql
     * @return array_row or false
     */
    public static function queryRow($sql){
        //调用SQL执行函数
        $res = self::dbQuery($sql);

        //从结果集中提取数据
        return mysql_fetch_assoc($res);
    }

    /**
     * @param $sql
     * @return array_list or array()
     */
    public static function queryAll($sql){
        $res = self::dbQuery($sql);
        $list = array();
        while($row =mysql_fetch_assoc($res)){
            $list[]=$row;
        }
        return $list;
    }

    /**
     *
     * @param $table：表名
     * @return bool|string：没有字段或所有字段的客串，用','隔开
     */
    public static function queryAllFields($table){

        $sql = "select * from #__$table";
        $rs = self::dbQuery($sql);
        $fields_num = mysql_num_fields($rs);
        if($fields_num){
            $fileds_name_all = '';
            for($i=0;$i<$fields_num;$i++){
                $fileds_name_all .= mysql_field_name($rs,$i).',';
            }
            $fileds_name_all = rtrim($fileds_name_all,',');
            return $fileds_name_all;
        }else{
            return false;
        }
    }


    /*
     * 构造表名
     * @param1 string $table = ''，要获取全名的表的名字
     * @return 带前缀的表全名
    */
/*    protected function getTable($table = ''){
        return ($table == '') ?  $this->prefix . $this->table : $this->prefix . $table;
        //父类的表前缀拼凑子类的表名属性
    }*/

    /**
     * 查询一下表中所有记录数
     * @param $table_name 表名不用加前缀
     */
    /*public function getCount($table_name=''){
        $sql = "select count(*) as c from {$this->getTable($table_name)}";
        $count = $this->queryRow($sql);
        return $count['c'];
    }*/

    public static function __callStatic($name,$value){
        echo "你访问的DB的::静态方法不存在$name,$value";
    }
}